package com.ttt.map;

import com.ttt.game.Bullet;
import com.ttt.util.BulletsPool;
import com.ttt.util.Constant;
import com.ttt.util.MyUtil;

import java.awt.*;
import java.util.List;

/**
 * 地图元素块
 */
public class MapWall {
    public static final int Type_Normal = 0;
    public static final int Type_House = 1;
    public static final int Type_Cover = 2;
    public static final int Type_Hard = 3;
    public static final int Type_Portals = 4;
    public static final int Type_River = 5;
    public static int wallW = 38;
    public static int wallH;
    public static int radius = wallW >> 1;
    private int type = Type_Normal;

    private static Image[] wallImg;
    static {
        wallImg = new Image[6];
        wallImg[Type_Normal] = MyUtil.createImage("images/wall.png");
        wallImg[Type_House] = MyUtil.createImage("images/Base_camp2.png");
        wallImg[Type_Cover] = MyUtil.createImage("images/cover.png");
        wallImg[Type_Hard] = MyUtil.createImage("images/Marble wall.png");
        wallImg[Type_River] = MyUtil.createImage("images/river.png");
        wallImg[Type_Portals] = MyUtil.createImage("images/Milky Way.png");
        if (wallW <= 0){
            wallW = wallImg[Type_Normal].getWidth(null);
        }
    }

    //图片资源的左上角坐标
    private int x,y;
    private boolean visible = true;

    public MapWall(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public MapWall() {

    }

    public void draw(Graphics g){
        if (!visible)
            return;;
        if (wallW <= 0){
            wallW = wallImg[Type_Normal].getWidth(null);
        }
        g.drawImage(wallImg[type],x,y,null);

        //绘制块上的名字
//        if (name != null){
//            g.setColor(Color.white);
//            g.drawString(name,x - radius,y + 10);
//        }
    }

    /**
     * 地图块和若干个子弹是否有碰撞
     * @param bullets
     * @return
     */
    public boolean isCollideBullet(List<Bullet> bullets){
        if (!visible || type == Type_Cover)
            return false;
//        for (Bullet bullet : bullets) { }
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            int bulletX = bullet.getX();
            int bulletY = bullet.getY();
            boolean collide = MyUtil.isCollide(x + radius, y + radius, radius, bulletX, bulletY);
            if (collide){
                //子弹的销毁
                bullet.setVisible(false);
                BulletsPool.theReturn(bullet);
                return true;
            }
        }
       return false;
    }

    //判断当前的地图块是否是老巢
    public boolean isHouse(){
        return type == Type_House;
    }

    @Override
    public String toString() {
        return "MapWall{" +
                "type=" + type +
                ", x=" + x +
                ", y=" + y +
                ", visible=" + visible +
                '}';
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public boolean isVisible() {
        return visible;
    }
    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}
